package com.toman.Adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.Enum.BaseEnum;
import com.toman.Enum.PermEnum;
import com.toman.JavaBean.ShiftEntity;
import com.toman.R;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;

import java.util.List;

/**
 * Created by 邹苏隆 on 2018/6/8.
 * description：
 */

public class ShiftAdapter extends AdapterBase<ShiftEntity, ShiftHoledr> implements View.OnClickListener {


    public interface ActionClickListener {
        public void onEditClick(ShiftEntity bean);

        public void onDelClick(ShiftEntity bean);

    }

    private LayoutInflater inflater;
    private Context mContext;

    private ActionClickListener actionClickListener;

    public ShiftAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }

    public void setActionClickListener(ActionClickListener l) {
        actionClickListener = l;
    }

    @Override
    protected void handlerData(List<ShiftEntity> mList2, int position, ShiftHoledr itemView) {
        ShiftEntity bean = mList2.get(position);
        itemView.tv_name.setText(bean.getShiftName());
        itemView.tv_time_start.setText(bean.getStartTime() );
        itemView.tv_time_end.setText(bean.getEndTime());
        itemView.iv_edit.setOnClickListener(this);
        itemView.iv_edit.setTag(position);
        itemView.iv_del.setOnClickListener(this);
        itemView.iv_del.setTag(position);
    }

    @Override
    protected ShiftHoledr getItemViewHolder() {
        return new ShiftHoledr();
    }

    @Override
    protected View initConvertView(View convertView, ShiftHoledr v) {
        convertView = inflater.inflate(R.layout.item_shift, null);
        v.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
        v.tv_time_start = (TextView) convertView.findViewById(R.id.tv_time_start);
        v.tv_time_end = (TextView) convertView.findViewById(R.id.tv_time_end);
        v.iv_edit = (ImageView) convertView.findViewById(R.id.iv_edit);
        v.iv_del = (ImageView) convertView.findViewById(R.id.iv_del);
        if(!PermissonUtils.contains(mContext,  BaseEnum.SHIFT.has(PermEnum.EDIT))){
            v.iv_edit.setVisibility(View.INVISIBLE);
        }
        if(!PermissonUtils.contains(mContext, BaseEnum.SHIFT.has(PermEnum.DELETE))){
            v.iv_del.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    @Override
    protected void onReachBottom() {

    }

    @Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        ShiftEntity bean = getItem(position);

        switch (view.getId()) {
            case R.id.iv_edit:
                if (actionClickListener != null) {
                    actionClickListener.onEditClick(bean);
                }
                break;
            case R.id.iv_del:
                if (actionClickListener != null) {
                    actionClickListener.onDelClick(bean);
                }
                break;

        }
    }


}    class ShiftHoledr {
    public ImageView iv_del;
    public ImageView iv_edit;
    public TextView tv_name;
    public TextView tv_time_start;
    public TextView tv_time_end;

}